package test._0512;


import java.util.Arrays;

public class QS {

    public static void quickSort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }

        quickSort(arr, 0, arr.length-1);
    }

    private static void quickSort(int[] arr, int l, int r) {
        if (l >= r) {
            return;
        }

        int i = l - 1, j = r + 1, x = i + j >> 1;
        while (i < j) {
            do i++;
            while (arr[i] < arr[x]);

            do j--;
            while (arr[j] > arr[x]);

            if (i < j ) {
                swap(arr, i, j);
            }
        }
        quickSort(arr, 0, j);
        quickSort(arr, j + 1, r);
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {4, 6, 23, 7, 2, 13};
        quickSort(arr);

        System.out.println(Arrays.toString(arr));
    }
}
